<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<div class="line-big">
	<div class="xl3">
		<div class="panel border-back">
			<div class="panel-head text-center">
				<div class="">
					<input type="text" class="input radius-rounded" id="keyword_of_goods_input" value="${param.k }" />
				</div>
				<div>
					<button id="search_goods_of_all_button" class="button border-blue radius-rounded margin-top">
						<span class="icon-search"></span> 搜全部
					</button>
					<button id="search_goods_of_this_type_button" class="button border-blue radius-rounded margin-top">
						<span class="icon-search"></span> 搜当前类别
					</button>
				</div>
			</div>
			<div class="panel-body text-center">
				<h3 class="">
					当前类别：<span class="text-white bg-blue radius-big" id="goods_type_id_div" data-value="">全部</span>
				</h3>
			</div>
			<div class="panel-foot bg-back border-back">
				<div class="content_wrap">
					<div class="zTreeDemoBackground left">
						<ul id="treeDemo" class="ztree"></ul>
					</div>
				</div>
			</div>
			<div class="panel-foot">
				<span class="icon-info-circle"></span> 选择最后一级分类，更有利于你的商品被找到。
			</div>
		</div>
	</div>
	<div class="xl9">
		<div class="panel border-back" id="write_goods_info_div">
			<div class="panel-body ">
				<div class="line-big">
					<div class="xl9 text-left">
						<button class="button border-blue" id="select_all_goods_id">
							<span class="icon-star"></span> 全选
						</button>
						<button class="button border-blue" id="reverse_select_all_goods_id">
							<span class="icon-star-half-empty"></span> 反选
						</button>
						<c:choose>
							<c:when test="${param.s=='准备中' }">
								<button class="button bg-red" id="batch_up_goods">
									<span class="icon-cloud-upload"></span> 批量上架
								</button>
							</c:when>
							<c:when test="${param.s=='销售中' }">
								<button class="button bg-red" id="batch_down_goods">
									<span class="icon-cloud-download"></span> 批量下架
								</button>
							</c:when>
							<c:when test="${param.s=='已下架' }">
								<button class="button bg-red" id="batch_up_goods">
									<span class="icon-cloud-upload"></span> 批量上架
								</button>
							</c:when>
						</c:choose>
					</div>
					<div class="xl3 text-right">
						<a class="button bg-blue" target="_blank" href="file/goods_export_record.xlsx">
							<span class="icon-download"></span> 下载全部商品Excel
						</a>
					</div>
				</div>

			</div>
			<div class="panel-foot bg-yellow-light border-back">
				<div class="line-big">
					<div class="xl5 text-center border-small border-right border-blue">商品信息</div>
					<div class="xl1 text-center border-small border-right border-blue">单价</div>
					<div class="xl1 text-center border-small border-right border-blue">库存</div>
					<div class="xl1 text-center border-small border-right border-blue">已卖出</div>
					<div class="xl2 text-center border-small border-right border-blue">商品详情</div>
					<div class="xl2 text-center">操作</div>
				</div>
			</div>
			<ul class="list-group list-striped">
				<c:forEach items="${goodsList}" var="g">
					<li id="li_${g.goodsId }">
						<div class="line-big">
							<div class="xl5 text-left">
								<h4 title="${g.goodsTips }">
									<input class="goods_id_check_box" type="checkbox" data-id="${g.goodsId }" data-li="#li_${g.goodsId }">
									<strong>${g.goodsName }</strong> <span class="badge bg-blue" title="所属类型：${goodsTypeMap[g.goodsTypeId].goodsTypeName}&#10;上级类型：${goodsTypeMap[goodsTypeMap[g.goodsTypeId].goodsTypeParentId].goodsTypeName}">
										<c:if test="${goodsTypeMap[goodsTypeMap[g.goodsTypeId].goodsTypeParentId].goodsTypeName!=null&&goodsTypeMap[goodsTypeMap[g.goodsTypeId].goodsTypeParentId].goodsTypeName!=''}">
							${goodsTypeMap[goodsTypeMap[g.goodsTypeId].goodsTypeParentId].goodsTypeName}&gt;
							</c:if> ${goodsTypeMap[g.goodsTypeId].goodsTypeName}
									</span>
									<c:if test="${g.goodsBrand!=null&&g.goodsBrand!='' }">
										<span class="badge bg-green" title="品牌：${g.goodsBrand }">${g.goodsBrand }</span>
									</c:if>
								</h4>
								<h6 class="text-left G-MarginTop5">商品编号：${g.goodsId }</h6>
								<c:if test="${g.goodsItemId!=null&&g.goodsItemId!='' }">
									<h6 class="text-left G-MarginTop5">商品货号：${g.goodsItemId }</h6>
								</c:if>
								<c:if test="${g.goodsHscode!=null&&g.goodsHscode!='' }">
									<h6 class="text-left G-MarginTop5">商品HS编码：${g.goodsHscode }</h6>
								</c:if>
								<c:if test="${g.goodsCiqGoodsNo!=null&&g.goodsCiqGoodsNo!='' }">
									<h6 class="text-left G-MarginTop5">国检备案号：${g.goodsCiqGoodsNo }</h6>
								</c:if>
								<c:if test="${g.goodsCustomsListNo!=null&&g.goodsCustomsListNo!='' }">
									<h6 class="text-left G-MarginTop5">海关备案号：${g.goodsCustomsListNo }</h6>
								</c:if>
							</div>
							<div class="xl1 text-center">
								<fmt:formatNumber value="${g.goodsPrice }" type="currency" pattern="#,##0.##" />
								<c:if test="${g.goodsOriginalPrice!=null }">
									<h6 class="text-center G-MarginTop5">
										原价:
										<fmt:formatNumber value="${g.goodsOriginalPrice}" type="currency" pattern="#,##0.##" />
									</h6>
								</c:if>
							</div>
							<div class="xl1 text-center">${g.goodsInventoryNumber }</div>
							<div class="xl1 text-center">${g.goodsSalesNumber }</div>
							<div class="xl2 text-center">
								<button class="button button-little bg-blue dialogs see_goods_detail_button" data-toggle="click" data-target="#see_goods_detail" data-mask="1" data-width="960px"
									data-url="shop/show_goods_detail?commonStr=${g.goodsId }">
									<span class="icon-eye"></span> 查看详情
								</button>
							</div>
							<div class="xl2 text-center">
								<c:choose>
									<c:when test="${param.s=='准备中' }">
										<button class="goods_need_update_button button button-little bg-blue" data-id="${g.goodsId }">
											<span class="icon-pencil"></span> 修改
										</button>
										<button class="up_goods_button button button-little bg-red" data-id="${g.goodsId }" data-li="#li_${g.goodsId }">
											<span class="icon-cloud-upload"></span> 上架
										</button>
										<button class="set_goods_special_commission_button button bg-blue button-little" data-id="${g.goodsId }" data-li="#li_${g.goodsId }">设置佣金</button>
									</c:when>
									<c:when test="${param.s=='销售中' }">
										<button class="goods_need_update_button button button-little bg-blue" data-id="${g.goodsId }">
											<span class="icon-pencil"></span> 修改
										</button>
										<button class="down_goods_button button button-little bg-red" data-id="${g.goodsId }" data-li="#li_${g.goodsId }">
											<span class="icon-cloud-download"></span> 下架
										</button>
										<button class="set_goods_special_commission_button button bg-blue button-little" data-id="${g.goodsId }" data-li="#li_${g.goodsId }">设置佣金</button>
									</c:when>
									<c:when test="${param.s=='已下架' }">
										<button class="goods_need_update_button button button-little bg-blue" data-id="${g.goodsId }">
											<span class="icon-pencil"></span> 修改
										</button>
										<button class="up_goods_button button button-little bg-red" data-id="${g.goodsId }" data-li="#li_${g.goodsId }">
											<span class="icon-cloud-upload"></span> 上架
										</button>
										<button class="set_goods_special_commission_button button bg-blue button-little" data-id="${g.goodsId }" data-li="#li_${g.goodsId }">设置佣金</button>
									</c:when>
								</c:choose>
							</div>
						</div>
					</li>
				</c:forEach>
			</ul>
			<div class="panel-foot bg-white border-back" id="goods_list_pages" data-url="pages_button" data-index="${page.currentIndex}" data-pages="${page.totalPages}"
				data-href="shop/show_goods_info_list?s=${param.s }&tid=${param.tid }&k=${param.k }&i="></div>
		</div>
	</div>
</div>
<div class="clearfix G-MarginTop100"></div>
<div id="rMenu">
	<ul>
		<li id="m_add_grade_1" onclick="add_grade_1_node();">增加一级分类</li>
		<li id="m_add_grade_2" onclick="add_grade_2_node();">增加下级分类</li>
		<li id="m_del" onclick="removeTreeNode();">删除</li>
		<li id="m_reame" onclick="renameTreeNode();">重命名</li>
	</ul>
</div>
<div style="display: none;" id="goodsTypeJson">${goodsTypeJson }</div>
<div style="display: none;" id="k" data-value="${param.k }"></div>
<div style="display: none;" id="tid" data-value="${param.tid }"></div>
<div id="see_goods_detail"></div>
<script type="text/javascript" src="js/pintuer.js"></script>
<script type="text/javascript">
	$(function()
	{

		//设置佣金
		$(".set_goods_special_commission_button").click(function()
		{
			$("#admin_div").load("shop/show_special_commission_info_list?goodsId=" + $(this).attr("data-id"), {}, function()
			{

			});
		});

		$(".goods_need_update_button").click(function()
		{
			$("#admin_div").load("shop/shop_update_goods?id=" + $(this).attr("data-id"), {}, function()
			{

			});
		});

		$("#select_all_goods_id").click(function()
		{
			$(".goods_id_check_box").attr("checked", true);
		});
		$("#reverse_select_all_goods_id").click(function()
		{
			$(".goods_id_check_box").each(function()
			{
				$(this).attr("checked", !this.checked);
			});
		});
		$("#batch_up_goods").click(function()
		{
			var ids = "";
			var num = 0;
			$("input.goods_id_check_box:checked").each(function()
			{
				ids += $(this).attr("data-id") + ",";
				num++;
			});
			if (ids.length <= 0)
			{
				alert("请先勾选需要操作的商品");
				return false;
			}
			ids = ids.substring(0, ids.length - 1);
			if (confirm("确认上架这 " + num + " 个商品吗?") == false)
			{
				return false;
			}

			$("#batch_up_goods").attr("disabled", "disabled");
			$.post("admin/m_change_goods_up",
			{
				"commonStr" : ids
			}, function(json)
			{
				if (json.success == false)
				{
					alert(json.info);
					$("#batch_up_goods").removeAttr("disabled");
				}
				else if (json.success == true)
				{
					$("#batch_up_goods").removeAttr("disabled");
					$("input.goods_id_check_box:checked").each(function()
					{
						$($(this).attr("data-li")).remove();
					});
				}
			},
			//返回类型
			"json");
		});
		$("#batch_down_goods").click(function()
		{
			var ids = "";
			var num = 0;
			$("input.goods_id_check_box:checked").each(function()
			{
				ids += $(this).attr("data-id") + ",";
				num++;
			});
			if (ids.length <= 0)
			{
				alert("请先勾选需要操作的商品");
				return false;
			}
			ids = ids.substring(0, ids.length - 1);
			if (confirm("确认下架这 " + num + " 个商品吗?") == false)
			{
				return false;
			}

			$("#batch_down_goods").attr("disabled", "disabled");
			$.post("admin/m_change_goods_down",
			{
				"commonStr" : ids
			}, function(json)
			{
				if (json.success == false)
				{
					alert(json.info);
					$("#batch_down_goods").removeAttr("disabled");
				}
				else if (json.success == true)
				{
					$("#batch_down_goods").removeAttr("disabled");
					$("input.goods_id_check_box:checked").each(function()
					{
						$($(this).attr("data-li")).remove();
					});
				}
			},
			//返回类型
			"json");
		});
		$(".up_goods_button").each(function()
		{
			$(this).click(function()
			{
				if (confirm("确认上架此商品吗?") == false)
				{
					return false;
				}
				var ids = "";
				ids = $(this).attr("data-id");
				var obj = $(this);

				obj.attr("disabled", "disabled");
				$.post("admin/m_change_goods_up",
				{
					"commonStr" : ids
				}, function(json)
				{
					if (json.success == false)
					{
						alert(json.info);
						obj.removeAttr("disabled");
					}
					else if (json.success == true)
					{
						obj.removeAttr("disabled");
						$(obj.attr("data-li")).remove();
					}
				},
				//返回类型
				"json");
			});
		});
		$(".down_goods_button").each(function()
		{
			$(this).click(function()
			{
				if (confirm("确认下架此商品吗?") == false)
				{
					return false;
				}
				var ids = "";
				ids = $(this).attr("data-id");
				var obj = $(this);

				obj.attr("disabled", "disabled");
				$.post("admin/m_change_goods_down",
				{
					"commonStr" : ids
				}, function(json)
				{
					if (json.success == false)
					{
						alert(json.info);
						obj.removeAttr("disabled");
					}
					else if (json.success == true)
					{
						obj.removeAttr("disabled");
						$(obj.attr("data-li")).remove();
					}
				},
				//返回类型
				"json");
			});
		});
		$("#goods_list_pages").load($("#goods_list_pages").attr("data-url"),
		{
			"page.currentIndex" : $("#goods_list_pages").attr("data-index"),
			"page.totalPages" : $("#goods_list_pages").attr("data-pages"),
			"page.href" : $("#goods_list_pages").attr("data-href")
		}, function()
		{
		});
		$("#goods_list_pages").on("click", "button[data-url]", function()
		{
			$("#admin_div").load(encodeURI($(this).attr("data-url")), {}, function()
			{
			});
		});

		$("#keyword_of_goods_input").keydown(function(event)
		{
			if (event.keyCode == 13)
			{
				$("#search_goods_of_all_button").trigger("click");
			}
		});

		$("#search_goods_of_all_button").click(function()
		{
			var keyword = $("#keyword_of_goods_input").val();
			if (keyword.length <= 0)
			{
				return false;
			}
			$("#admin_div").load(encodeURI("shop/show_goods_info_list?s=${param.s}&k=" + keyword), {}, function()
			{
			});
		});
		$("#search_goods_of_this_type_button").click(function()
		{
			var keyword = $("#keyword_of_goods_input").val();
			if (keyword.length <= 0)
			{
				return false;
			}
			if ($("#goods_type_id_div").attr("data-value").length <= 0)
			{
				toastr.warning("请先选择一个类别", '提示!');
				return false;
			}
			var node = zTree.getNodeByParam("id", $("#goods_type_id_div").attr("data-value"), null);
			if (node == null)
			{
				toastr.warning("请先选择类别", '提示!');
				return false;
			}
			var ids = [];
			ids = getChildren(ids, node);
			$("#admin_div").load(encodeURI("shop/show_goods_info_list?s=${param.s}&tid=" + ids + "&k=" + keyword), {}, function()
			{
			});
		});

	});

	var setting =
	{
		view :
		{
			dblClickExpand : false,
			showLine : false
		},
		data :
		{
			simpleData :
			{
				enable : true
			}
		},
		view :
		{
			selectedMulti : false
		},
		callback :
		{
			onClick : onClick,
			onRightClick : OnRightClick
		}
	};

	var zNodes = eval('(' + $("#goodsTypeJson").html() + ')');

	function onClick(e, treeId, treeNode)
	{
		var zTree = $.fn.zTree.getZTreeObj("treeDemo");
		//zTree.expandNode(treeNode);
		$("#goods_type_id_div").html(treeNode.name);
		$("#goods_type_id_div").attr("data-value", treeNode.id);
		var ids = [];
		ids = getChildren(ids, treeNode);
		$("#admin_div").load(encodeURI("shop/show_goods_info_list?s=${param.s}&tid=" + ids.toString()), {}, function()
		{
		});
	}
	function OnRightClick(event, treeId, treeNode)
	{
		/* if (!treeNode && event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 0)
		{
			zTree.cancelSelectedNode();
			showRMenu("root", event.clientX, event.clientY);
		}
		else if (treeNode && !treeNode.noR)
		{
			zTree.selectNode(treeNode);
			//alert("node" + treeNode.grade+","+("node" + treeNode.grade).length);
			showRMenu("node" + treeNode.grade, event.clientX, event.clientY);
		} */
	}
	function showRMenu(type, x, y)
	{
		$("#rMenu ul").show();
		if (type == "root")
		{
			$("#m_add_grade_1").show();
			$("#m_add_grade_2").hide();
			$("#m_del").hide();
			$("#m_reame").hide();
		}
		else
		{
			$("#m_del").show();
			$("#m_reame").show();
			if (type == "node1")
			{
				$("#m_add_grade_1").hide();
				$("#m_add_grade_2").show();
			}
			if (type == "node2")
			{
				$("#m_add_grade_1").hide();
				$("#m_add_grade_2").show();
			}
			if (type == "node3")
			{
				$("#m_add_grade_1").hide();
				$("#m_add_grade_2").hide();
			}
		}
		rMenu.css(
		{
			"top" : y + "px",
			"left" : x + "px",
			"visibility" : "visible"
		});

		$("body").bind("mousedown", onBodyMouseDown);
	}
	function add_grade_1_node()
	{
		hideRMenu();
		var type_name_str = prompt("请输入类型名称,批量添加中间用英文逗号,隔开");
		if (type_name_str.length <= 0)
		{
			alert("不满足类型名称要求");
			return false;
		}
		$.post("goods_type/goods_type_add_do",
		{
			"goodsType.goodsTypeName" : type_name_str
		}, function(json)
		{
			if (json.success == false)
			{
				alert(json.info);
			}
			else
			{
				$.each(json.goodsType, function(i, n)
				{
					var newNode =
					{
						id : n.goodsTypeId,
						name : n.goodsTypeName,
						pId : n.goodsTypeParentId,
						grade : 1
					};
					zTree.addNodes(null, newNode);
				});
			}
		},
		//返回类型
		"json");
	}
	function add_grade_2_node()
	{
		hideRMenu();
		var type_name_str = prompt("请输入类型名称");
		if (type_name_str.length <= 0)
		{
			alert("不满足类型名称要求");
			return false;
		}
		var newNode =
		{
			name : type_name_str
		};
		if (zTree.getSelectedNodes()[0])
		{
			$.post("goods_type/goods_type_add_do",
			{
				"goodsType.goodsTypeName" : type_name_str,
				"goodsType.goodsTypeParentId" : zTree.getSelectedNodes()[0].id
			}, function(json)
			{
				if (json.success == false)
				{
					alert(json.info);
				}
				else
				{

					$.each(json.goodsType, function(i, n)
					{
						var newNode =
						{
							id : n.goodsTypeId,
							name : n.goodsTypeName,
							pId : n.goodsTypeParentId,
							grade : parseInt(zTree.getSelectedNodes()[0].grade) + 1
						};
						newNode.checked = zTree.getSelectedNodes()[0].checked;
						zTree.addNodes(zTree.getSelectedNodes()[0], newNode);
					});
				}
			},
			//返回类型
			"json");
		}
	}
	function removeTreeNode()
	{
		hideRMenu();
		var nodes = zTree.getSelectedNodes();
		if (nodes && nodes.length > 0)
		{
			if (nodes[0].children && nodes[0].children.length > 0)
			{
				var msg = "要删除的节点是父节点，如果删除将连同子节点一起删掉。\n\n请确认！";
				if (confirm(msg) == true)
				{
					var ids = [];
					ids = getChildren(ids, nodes[0]);
					$.post("goods_type/goods_type_delete_do",
					{
						"commonStr" : ids.toString()
					}, function(json)
					{
						if (json.success == false)
						{
							alert(json.info);
						}
						else
						{
							zTree.removeNode(nodes[0]);
						}
					},
					//返回类型
					"json");
				}

			}
			else
			{
				var msg = "真的要删除此节点吗 ?\n\n请确认！";
				if (confirm(msg) == true)
				{
					$.post("goods_type/goods_type_delete_do",
					{
						"commonStr" : nodes[0].id
					}, function(json)
					{
						if (json.success == false)
						{
							alert(json.info);
						}
						else
						{
							zTree.removeNode(nodes[0]);
						}
					},
					//返回类型
					"json");
				}
			}
		}
	}

	//ids是一个数组 返回结果数组     treeNode是选中的节点
	function getChildren(ids, treeNode)
	{
		ids.push(treeNode.id);
		if (treeNode.isParent)
		{
			for ( var obj in treeNode.children)
			{
				getChildren(ids, treeNode.children[obj]);
			}
		}
		return ids;
	}
	function renameTreeNode()
	{
		hideRMenu();
		var type_name_str = prompt("请输入类型名称");
		if (type_name_str.length <= 0)
		{
			alert("不满足类型名称要求");
			return false;
		}
		var nodes = zTree.getSelectedNodes();
		if (nodes && nodes.length > 0)
		{
			$.post("goods_type/goods_type_update_do",
			{
				"goodsType.goodsTypeId" : nodes[0].id,
				"goodsType.goodsTypeName" : type_name_str
			}, function(json)
			{
				if (json.success == false)
				{
					alert(json.info);
				}
				else
				{
					nodes[0].name = type_name_str;
					zTree.updateNode(nodes[0]);
				}
			},
			//返回类型
			"json");
		}
	}

	function hideRMenu()
	{
		if (rMenu)
			rMenu.css(
			{
				"visibility" : "hidden"
			});
		$("body").unbind("mousedown", onBodyMouseDown);
	}
	function onBodyMouseDown(event)
	{
		if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length > 0))
		{
			rMenu.css(
			{
				"visibility" : "hidden"
			});
		}
	}
	var zTree, rMenu;
	$(document).ready(function()
	{
		$.fn.zTree.init($("#treeDemo"), setting, zNodes);
		zTree = $.fn.zTree.getZTreeObj("treeDemo");
		rMenu = $("#rMenu");
		if ($("#tid").attr("data-value").length > 0)
		{
			var end = $("#tid").attr("data-value").indexOf(",") >= 0 ? $("#tid").attr("data-value").indexOf(",") : $("#tid").attr("data-value").length;
			var node = zTree.getNodeByParam("id", $("#tid").attr("data-value").substring(0, end), null);
			if (node != null)
			{
				zTree.selectNode(node);
				$("#goods_type_id_div").html(node.name);
				$("#goods_type_id_div").attr("data-value", node.id);
			}
		}
	});
</script>